package com.weishi.project.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.weishi.common.enums.RedisConstants;
import com.weishi.common.enums.TrainingProgramEnum;
import com.weishi.common.utils.DateUtils;
import com.weishi.common.utils.StringUtils;
import com.weishi.framework.redis.RedisCache;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.framework.web.page.TableDataInfo;
import com.weishi.project.api.ret.NaturalYearRet;
import com.weishi.project.api.service.ApiWeekPlanService;
import com.weishi.project.api.vo.*;
import com.weishi.project.api.vo.appvo.GetWaterDataVO;
import com.weishi.project.speedometer.vo.CurrentShipSpeedVo;
import com.weishi.project.speedometer.vo.DevInfo;
import com.weishi.project.training.domain.*;
import com.weishi.project.training.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/app/weekPlan")
@Api(value = "weekPlan", description = "周计划")
public class ApiWeekPlanController extends BaseController {

    @Autowired
    private INaturalYearService naturalYearService;

    @Autowired
    private ITrainingWeekService trainingWeekService;

    @Autowired
    private ITrainingDayService trainingDayService;

    @Autowired
    private ITrainingGroupService trainingGroupService;

    @Autowired
    private ITrainingGroupChannelService trainingGroupChannelService;

    @Autowired
    private ISignInRecordService signInRecordService;

    @Autowired
    private ILeaveFormService leaveFormService;

    @Autowired
    private ITrainingAchievementService trainingAchievementService;

    @Autowired
    private IAnnualProjectService annualProjectService;

    @Autowired
    private IShipSpeedRecordLogService shipSpeedRecordLogService;

    /**
     * 训练方案 主表 接口
     */
    @Autowired
    private ISchemeService schemeService;

    /**
     * 心率表 接口
     */
    @Autowired
    private IHeartRateService heartRateService;

    @Autowired
    private ISportsmanService sportsmanService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 水上分段数据
     */
    @Autowired
    private IHeartRateCountService heartRateCountService;


    @ApiOperation(value = "获取年度下拉数据")
    @GetMapping("/naturalYearList")
    public AjaxResult naturalYearList() {
        List<NaturalYearRet> list = naturalYearService.selectNaturalYearRetList(false);
        return AjaxResult.success(list);
    }

    /**
     * 获取周基本信息
     */
    @ApiOperation(value = "获取周基本信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam Integer annualPlanId, @RequestParam Integer id) {
        TrainingWeek trainingWeek = trainingWeekService.selectTrainingWeekByWeek(annualPlanId, id);
        return AjaxResult.success(trainingWeek);
    }

    /**
     * 修改周基本信息
     */
    @ApiOperation(value = "修改周基本信息")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody TrainingWeek trainingWeek) {
        TrainingWeek db = trainingWeekService.selectTrainingWeekByWeek(trainingWeek.getAnnualPlanId(), trainingWeek.getWeek());
        if (db != null) {
            trainingWeek.setId(db.getId());
            trainingWeek.setCtime(new Date());
            trainingWeekService.updateTrainingWeek(trainingWeek);
        } else {
            trainingWeek.setCtime(new Date());
            trainingWeekService.save(trainingWeek);
        }
        return AjaxResult.success();
    }

    /********************************************* 签到-考勤 **********************************************/
    @ApiOperation(value = "签到")
    @PostMapping("/signIn")
    public AjaxResult signIn(@RequestBody SignInRecord signInRecord) {
        TrainingWeek trainingWeek = trainingWeekService.selectTrainingWeekByDay(new Date());
        if (trainingWeek != null) {
            signInRecord.setWeekId(trainingWeek.getId());
            signInRecordService.save(signInRecord);
        } else {
            return AjaxResult.error("今天没有训练计划");
        }
        return AjaxResult.success();
    }

    @ApiOperation(value = "请假")
    @PostMapping("/leave")
    public AjaxResult leave(@RequestBody Map<String, List<LeaveForm>> map) {

        try {
            Date now = new Date();
            for (LeaveForm leaveForm : map.get("leaveForms")) {
                if (leaveForm.getEndTime() == null) {
                    leaveForm.setEndTime(now);
                }
                leaveForm.setStartTime(now);
                leaveForm.setCtime(now);
                leaveForm.setLtime(now);
                leaveFormService.save(leaveForm);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("参数异常");
        }
        return AjaxResult.success();
    }

    @ApiOperation(value = "请假-进入训练")
    @PostMapping("/endOfLeave")
    public AjaxResult endOfLeave(@RequestBody LeaveForm leaveForm) {
        Date endTime = DateUtils.getSpecifiedDayAfter(new Date(), -1);
        leaveForm.setCureTime(endTime);
        leaveFormService.updateBySportsmanId(leaveForm);
        return AjaxResult.success();
    }


    @ApiOperation(value = "同步其他周训练计划")
    @GetMapping("/synchronizeData")
    public AjaxResult synchronizeData(@RequestParam Integer oldWeekId, @RequestParam Integer weekId) {
        String ret = this.trainingWeekService.synchronizeData(oldWeekId, weekId);
        return AjaxResult.success(ret);
    }

    @ApiOperation(value = "查询训练日项目列表")
    @PostMapping("/trainingDay/list")
    public AjaxResult list(@RequestBody TrainingDayVo trainingDayVo) throws ParseException {
        TrainingDay trainingDay = new TrainingDay();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        trainingDay.setCtime(new Date());
        trainingDay.setDay(trainingDayVo.getDay());
        trainingDay.setId(trainingDayVo.getId());
        trainingDay.setWeekId(trainingDayVo.getWeekId());
        trainingDay.setSr(trainingDayVo.getSr());
        trainingDay.setSecondLevel(trainingDayVo.getSecondLevel());
        trainingDay.setNumberGroups(trainingDayVo.getNumberGroups());
        trainingDay.setHr(trainingDayVo.getHr());
        trainingDay.setGapTime(trainingDayVo.getGapTime());
        trainingDay.setFourSeasons(trainingDayVo.getFourSeasons());
        trainingDay.setFirstLevel(trainingDayVo.getFirstLevel());
        trainingDay.setClassHour(trainingDayVo.getClassHour());
        trainingDay.setDayWeek(trainingDayVo.getDayWeek());
        trainingDay.setEndTime(trainingDayVo.getEndTime());
        trainingDay.setIdealKilometers(trainingDayVo.getIdealKilometers());
        trainingDay.setKilometers(trainingDayVo.getKilometers());
        trainingDay.setScore(trainingDayVo.getScore());
        trainingDay.setStartTime(trainingDayVo.getStartTime());
        trainingDay.setStrength(trainingDayVo.getStrength());
        trainingDay.setWeekName(trainingDayVo.getWeekName());
        trainingDay.setFirstLevelName(trainingDayVo.getFirstLevelName());
        trainingDay.setSecondLevelName(trainingDayVo.getSecondLevelName());
        List<TrainingDay> list = trainingDayService.selectTrainingDayList(trainingDay);
        return AjaxResult.success(list);
    }


    @ApiOperation(value = "本周目标")
    @GetMapping("/trainingDay/listWeek")
    public AjaxResult listWeek(String day) throws ParseException {
//        List<TrainingDay> list = trainingDayService.selectTrainingDayListByDay(day);
        List<TrainingDay> list1 = trainingDayService.selectTrainingDayBeforedawn(day);
        List<TrainingDay> list2 = trainingDayService.selectTrainingDayMorning(day);
        List<TrainingDay> list3 = trainingDayService.selectTrainingDayAfternoon(day);
        List<TrainingDay> list4 = trainingDayService.selectTrainingDayNight(day);
        Map<String, List<TrainingDaySchemeListVO>> map = new HashMap<>();

        // 查询黎明的数据
        List<TrainingDaySchemeListVO> trainingDaySchemeListVOList1 = new ArrayList<>();
        list1.forEach(p -> {
            System.out.println();
            TrainingDaySchemeListVO trainingDaySchemeListVO = new TrainingDaySchemeListVO();
            StringUtils.copyProperties(p, trainingDaySchemeListVO);
            if (StringUtils.isNotNull(p.getSchemeId())) {
                System.out.println();
                Scheme scheme = schemeService.getById(p.getSchemeId());
                if (StringUtils.isNotNull(scheme)) {
                    trainingDaySchemeListVO.setName(scheme.getName());
                    trainingDaySchemeListVO.setTargetValue(scheme.getTargetValue());
                    trainingDaySchemeListVO.setGroupNumber(scheme.getGroupNumber());
                    trainingDaySchemeListVO.setGapTime(Integer.parseInt(scheme.getIntervalTime()));
                }
            }
            trainingDaySchemeListVOList1.add(trainingDaySchemeListVO);
        });
        map.put("beforedawn", trainingDaySchemeListVOList1);

        // 查询上午的数据
        List<TrainingDaySchemeListVO> trainingDaySchemeListVOList2 = new ArrayList<>();
        list2.forEach(p2 -> {
            TrainingDaySchemeListVO trainingDaySchemeListVO2 = new TrainingDaySchemeListVO();
            StringUtils.copyProperties(p2, trainingDaySchemeListVO2);
            if (StringUtils.isNotNull(p2.getSchemeId())) {
                System.out.println();
                Scheme scheme2 = schemeService.getById(p2.getSchemeId());
                if (StringUtils.isNotNull(scheme2)) {
                    System.out.println();
                    trainingDaySchemeListVO2.setName(scheme2.getName());
                    trainingDaySchemeListVO2.setTargetValue(scheme2.getTargetValue());
                    trainingDaySchemeListVO2.setGroupNumber(scheme2.getGroupNumber());
                    trainingDaySchemeListVO2.setGapTime(Integer.parseInt(scheme2.getIntervalTime()));
                }
            }
            trainingDaySchemeListVOList2.add(trainingDaySchemeListVO2);
        });
        map.put("morning", trainingDaySchemeListVOList2);

        //查询下午的数据
        List<TrainingDaySchemeListVO> trainingDaySchemeListVOList3 = new ArrayList<>();
        list3.forEach(p3 -> {
            TrainingDaySchemeListVO trainingDaySchemeListVO3 = new TrainingDaySchemeListVO();
            StringUtils.copyProperties(p3, trainingDaySchemeListVO3);
            if (StringUtils.isNotNull(p3.getSchemeId())) {
                Scheme scheme3 = schemeService.getById(p3.getSchemeId());
                if (StringUtils.isNotNull(scheme3)) {
                    trainingDaySchemeListVO3.setName(scheme3.getName());
                    trainingDaySchemeListVO3.setTargetValue(scheme3.getTargetValue());
                    trainingDaySchemeListVO3.setGroupNumber(scheme3.getGroupNumber());
                    trainingDaySchemeListVO3.setGapTime(Integer.parseInt(scheme3.getIntervalTime()));
                }
            }
            trainingDaySchemeListVOList3.add(trainingDaySchemeListVO3);
        });
        map.put("afternoon", trainingDaySchemeListVOList3);

        //查询晚上的数据
        List<TrainingDaySchemeListVO> trainingDaySchemeListVOList4 = new ArrayList<>();
        list4.forEach(p4 -> {
            TrainingDaySchemeListVO trainingDaySchemeListVO4 = new TrainingDaySchemeListVO();
            StringUtils.copyProperties(p4, trainingDaySchemeListVO4);
            if (StringUtils.isNotNull(p4.getSchemeId())) {
                Scheme scheme4 = schemeService.getById(p4.getSchemeId());
                if (StringUtils.isNotNull(scheme4)) {
                    trainingDaySchemeListVO4.setName(scheme4.getName());
                    trainingDaySchemeListVO4.setTargetValue(scheme4.getTargetValue());
                    trainingDaySchemeListVO4.setGroupNumber(scheme4.getGroupNumber());
                    trainingDaySchemeListVO4.setGapTime(Integer.parseInt(scheme4.getIntervalTime()));
                }
            }
            trainingDaySchemeListVOList4.add(trainingDaySchemeListVO4);
        });
        map.put("night", trainingDaySchemeListVOList4);
        return AjaxResult.success(map);
    }


    @ApiOperation(value = "查询训练日项目详情")
    @GetMapping("/trainingDay/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingDayService.getTrainingDayById(id));
    }

    @ApiOperation(value = "训练日项目的天气情况")
    @PostMapping("/trainingDay/updateEnvironmental")
    public AjaxResult updateEnvironmental(@RequestBody TrainingDay training) throws Exception {
        training.setTrainingMode(trainingDayService.getById(training.getId()).getTrainingMode());
        return AjaxResult.success(trainingDayService.updateById(training));
    }

    @ApiOperation(value = "新增训练日项目")
    @PostMapping("/trainingDay/add")
    public AjaxResult add(@RequestBody Map<String, List<TrainingDayVo>> trainingDayList) throws Exception {
        TrainingDay trainingDay = new TrainingDay();
        for (TrainingDayVo trainingDayVo : trainingDayList.get("trainingDays")) {
            trainingDay.setCtime(new Date());
            trainingDay.setDay(trainingDayVo.getDay());
            trainingDay.setId(trainingDayVo.getId());
            trainingDay.setWeekId(trainingDayVo.getWeekId());
            trainingDay.setSr(trainingDayVo.getSr());
            trainingDay.setSrMemo(trainingDayVo.getSrMemo());
            trainingDay.setSrTwo(trainingDayVo.getSrTwo());
            trainingDay.setSecondLevel(trainingDayVo.getSecondLevel());
            trainingDay.setNumberGroups(trainingDayVo.getNumberGroups());
            trainingDay.setHr(trainingDayVo.getHr());
            trainingDay.setHrTwo(trainingDayVo.getHrTwo());
            trainingDay.setGapTime(trainingDayVo.getGapTime());
            trainingDay.setFourSeasons(trainingDayVo.getFourSeasons());
            trainingDay.setFirstLevel(trainingDayVo.getFirstLevel());
            trainingDay.setClassHour(trainingDayVo.getClassHour());
            trainingDay.setRealClassHour(trainingDayVo.getRealClassHour());
            trainingDay.setDayWeek(trainingDayVo.getDayWeek());
            trainingDay.setEndTime(trainingDayVo.getEndTime());
            trainingDay.setIdealKilometers(trainingDayVo.getIdealKilometers());
            trainingDay.setWarmUpKilometers(trainingDayVo.getWarmUpKilometers());
            trainingDay.setKilometers(trainingDayVo.getKilometers());
            trainingDay.setScore(trainingDayVo.getScore());
            trainingDay.setStartTime(trainingDayVo.getStartTime());
            trainingDay.setStrength(trainingDayVo.getStrength());
            trainingDay.setWeekName(trainingDayVo.getWeekName());
            trainingDay.setFirstLevelName(trainingDayVo.getFirstLevelName());
            trainingDay.setSecondLevelName(trainingDayVo.getSecondLevelName());
            trainingDay.setTrainingMode(trainingDayVo.getTrainingMode());
            trainingDay.setSchemeId(trainingDayVo.getSchemeId());
            trainingDayService.save(trainingDay);

            /**
             * 创建 训练组
             */
            /*TrainingGroup trainingGroup = new TrainingGroup();
            for(int i = 0; i < trainingDayVo.getNumberGroups(); i++){
                StringUtils.copyProperties(trainingDayVo , trainingGroup);
                trainingGroup.setTrainingDayId(trainingDay.getId());
                trainingGroup.setType(trainingDayVo.getFirstLevel());
                trainingGroup.setCtime(new Date());
                trainingGroup.setKilometer(trainingDayVo.getIdealKilometers());
                trainingGroup.setGroupName("第"+(i+1)+"组");
                trainingGroupService.save(trainingGroup);
            }*/
        }
        return AjaxResult.success();
    }

    @ApiOperation(value = "新增训练日项目和关联周信息")
    @PostMapping("/trainingDay/addWeekAndDay")
    public AjaxResult addWeekAndDay(@RequestBody AddWeekAndDayVo addWeekAndDayVo) {

        TrainingWeek trainingWeek = new TrainingWeek();
        trainingWeek.setId(addWeekAndDayVo.getId());
        trainingWeek.setAnnualPlanId(addWeekAndDayVo.getAnnualPlanId());
        trainingWeek.setFitnessCoach(addWeekAndDayVo.getFitnessCoach());
        trainingWeek.setHeadCoach(addWeekAndDayVo.getHeadCoach());
        trainingWeek.setSpecialCoach(addWeekAndDayVo.getSpecialCoach());
        trainingWeek.setTeamDoctor(addWeekAndDayVo.getTeamDoctor());
        trainingWeek.setTeamLeader(addWeekAndDayVo.getTeamLeader());
        trainingWeek.setTrainingLocationId(addWeekAndDayVo.getTrainingLocationId());
        trainingWeek.setType(addWeekAndDayVo.getType());
        trainingWeekService.saveOrUpdate(trainingWeek);

        /*TrainingDayFourseansonsVo trainingDayFourseansonsVo = addWeekAndDayVo.getWeekPlanTrainingDayLis();
        if (trainingDayFourseansonsVo.getMorning().size() > 0) {
            if (trainingDayFourseansonsVo.getMorning().get(0).getId() == null) {
                for (TrainingDay trainingDay : trainingDayFourseansonsVo.getMorning()) {
                    trainingDay.setCtime(new Date());
                    trainingDay.setKilometers(0);
                    trainingDay.setCompletion(0);
                    trainingDayService.saveOrUpdate(trainingDay);
                }
            }
        }
        if (trainingDayFourseansonsVo.getBeforedawn().size() > 0) {
            if (trainingDayFourseansonsVo.getBeforedawn().get(0).getId() == null) {
                for (TrainingDay trainingDay : trainingDayFourseansonsVo.getBeforedawn()) {
                    trainingDay.setCtime(new Date());
                    trainingDay.setKilometers(0);
                    trainingDay.setCompletion(0);
                    trainingDayService.saveOrUpdate(trainingDay);
                }
            }
        }
        if (trainingDayFourseansonsVo.getAfternoon().size() > 0) {
            if (trainingDayFourseansonsVo.getAfternoon().get(0).getId() == null) {
                for (TrainingDay trainingDay : trainingDayFourseansonsVo.getAfternoon()) {
                    trainingDay.setCtime(new Date());
                    trainingDay.setKilometers(0);
                    trainingDay.setCompletion(0);
                    trainingDayService.saveOrUpdate(trainingDay);
                }
            }
        }
        if (trainingDayFourseansonsVo.getNight().size() > 0) {
            if (trainingDayFourseansonsVo.getNight().get(0).getId() == null) {
                for (TrainingDay trainingDay : trainingDayFourseansonsVo.getNight()) {
                    trainingDay.setCtime(new Date());
                    trainingDay.setKilometers(0);
                    trainingDay.setCompletion(0);
                    trainingDayService.saveOrUpdate(trainingDay);
                }
            }
        }*/

        return AjaxResult.success();
    }

    @ApiOperation(value = "修改训练日项目")
    @PostMapping("/trainingDay/edit")
    public AjaxResult edit(@RequestBody Map<String, TrainingDay> params) {
        trainingDayService.updateTrainingWeekById(params.get("trainingDay"));
        return AjaxResult.success();
    }

    @ApiOperation(value = "删除训练日项目")
    @GetMapping("/trainingDay/remove/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        boolean ret = trainingDayService.removeByIds(Arrays.asList(ids));
        if(ret){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("删除失败");
        }
    }

    @ApiOperation(value = "水上(陆上)训练——查询训练小组列表")
    @GetMapping("/list")
    public TableDataInfo listTrainingGroup(TrainingGroup trainingGroup) {
        startPage();
        List<TrainingGroup> list = trainingGroupService.selectTrainingGroupList(trainingGroup);
        return getDataTable(list);
    }

    @ApiOperation(value = "水上(陆上)训练——查询训练小组详情")
    @GetMapping("/{id}")
    public AjaxResult getTrainingGroupInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingGroupService.getById(id));
    }

    @ApiOperation(value = "水上(陆上)训练——新增训练小组")
    @PostMapping("/trainingGroup/add")
    public AjaxResult addTrainingGroup(@RequestBody TrainingGroupVo trainingGroupVo) throws ParseException {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date day2 = simpleDateFormat.parse(trainingGroupVo.getDay());
        System.out.println(trainingGroupVo.getDay());
        Map<String, Object> map2 = new HashMap<>();
        map2.put("fourSeasons", trainingGroupVo.getFourSeasons());
        map2.put("firstLevel", trainingGroupVo.getFirstLevel());
        map2.put("day", trainingGroupVo.getDay());
        int trainingDayId = trainingGroupVo.getId();

        for (TrainingGroup trainingGroup : trainingGroupVo.getTrainingGroup()) {
            trainingGroup.setCtime(new Date());
            trainingGroup.setWeekId(trainingGroupVo.getWeekId());
            trainingGroup.setType(trainingGroupVo.getFirstLevel());
            trainingGroup.setTrainingDayId(trainingDayId);
            if (trainingGroup.getId() == -1) {
                trainingGroupService.save(trainingGroup);
            }
        }

        Map<String, Object> map3 = new HashMap<>();
        map3.put("weekId", trainingGroupVo.getWeekId());
        map3.put("trainingDayId", trainingDayId);
//        trainingGroupService.getTrainingGroup(map3);
        return AjaxResult.success(trainingGroupService.getTrainingGroup(map3));
    }

    @ApiOperation(value = "水上(陆上)训练——查看训练小组详情")
    @PostMapping("/trainingGroup/getDetails")
    public AjaxResult getDetails(@RequestBody TrainingGroupVo trainingGroupVo) throws ParseException {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date day2 = simpleDateFormat.parse(trainingGroupVo.getDay());
        Map<String, Object> map2 = new HashMap<>();
        map2.put("fourSeasons", trainingGroupVo.getFourSeasons());
        map2.put("firstLevel", trainingGroupVo.getFirstLevel());
        map2.put("day", trainingGroupVo.getDay());
        int trainingDayId = trainingGroupVo.getId();

        Map<String, Object> map3 = new HashMap<>();
        map3.put("weekId", trainingGroupVo.getWeekId());
        map3.put("trainingDayId", trainingDayId);
        return AjaxResult.success(trainingGroupService.getTrainingGroup(map3));
    }

    @ApiOperation(value = "水上(陆上)训练——查看训练小组中存在航道成绩的条数")
    @PostMapping("/trainingGroup/selectAchievementIsNull")
    public AjaxResult selectAchievementIsNull(@RequestBody Map<String, Object> params) {
        boolean ret = false;
        try{
            ret = trainingGroupChannelService.selectAchievementIsNull(params);
        }catch (Exception e){
            ret = false;
        }
        return AjaxResult.success(ret);
    }

    @ApiOperation(value = "水上(陆上)训练——查看训练小组航道成绩排名")
    @PostMapping("/trainingGroup/queryChannelAchievement")
    public AjaxResult queryChannelAchievement(@RequestBody ChannelAchievementParamsVo channelAchievementParamsVo) {
        Map<String, Object> params = new HashMap<>();
        params.put("day", channelAchievementParamsVo.getDay());
        params.put("firstLevel", channelAchievementParamsVo.getFirstLevel());
        params.put("fourSeasons", channelAchievementParamsVo.getFourSeasons());
        params.put("weekId", channelAchievementParamsVo.getWeekId());
        params.put("trainingDayId", channelAchievementParamsVo.getId());
        if (!channelAchievementParamsVo.getFirstLevel().equals("land_specific_training_dynamometer"))
            params.put("boatType", channelAchievementParamsVo.getBoatType());
        params.put("rowingKilometers", channelAchievementParamsVo.getRowingKilometers());
        params.put("rowingClassHour", channelAchievementParamsVo.getRowingClassHour());
        params.put("trainingMode", channelAchievementParamsVo.getTrainingMode());
        List<ChannelAchievementVo> channelAchievementVos = trainingGroupChannelService.queryChannelAchievement(params);
        Map<Integer, List<ChannelAchievementVo>> map = channelAchievementVos.stream().collect(Collectors.toMap(ChannelAchievementVo::getChannelId,
                p -> {
                    List<ChannelAchievementVo> itemList = new ArrayList<>();
                    itemList.add(p);
                    return itemList;
                },
                (List<ChannelAchievementVo> value1, List<ChannelAchievementVo> value2) -> {
                    value1.addAll(value2);
                    return value1;
                }
        ));
        List<ChannelAchievementReturnVo> list2 = new ArrayList<>();
        List<String> achievements = new ArrayList<>();
        for (Map.Entry<Integer, List<ChannelAchievementVo>> entry : map.entrySet()) {
            ChannelAchievementReturnVo channelAchievementReturnVo = new ChannelAchievementReturnVo();
            channelAchievementReturnVo.setChannelId(entry.getKey());
            channelAchievementReturnVo.setChannelAchievementVosList(entry.getValue());
            channelAchievementReturnVo.setAchieivement(entry.getValue().get(0).getAchievement());
            achievements.add(channelAchievementReturnVo.getAchieivement());
            if (channelAchievementParamsVo.getGroupName().equals(entry.getValue().get(0).getGroupName())) {
                list2.add(channelAchievementReturnVo);
            }
        }


        // 查询水上分段浆频
        if(channelAchievementParamsVo.getFirstLevel().equals(TrainingProgramEnum.WATERPROJECT.getKey())){
            // 查询并且计算平均浆频
            for (int i = 0; i<list2.size(); i++){
                for (int j = 0; j<list2.get(i).getChannelAchievementVosList().size(); j++){
                    Integer propellerFrequency = 0;
                    List<HeartRateCount> heartRateCountList = heartRateCountService
                            .listZHKID(list2.get(i).getChannelAchievementVosList().get(j).getChannelId());
                    int size = heartRateCountList.size();
                    for(int k = 0; k<size; k++){
                        propellerFrequency += heartRateCountList.get(k).getPropellerFrequency()==null?0:Integer.parseInt(heartRateCountList.get(k).getPropellerFrequency());
                    }
                    if(size > 0 && propellerFrequency > 0){
                        list2.get(i).getChannelAchievementVosList().get(j).setPropellerFrequency(propellerFrequency / size);
                    }
                }
            }
        }


        /**
         * 对 List 数组进行排序
         */
        if (channelAchievementParamsVo.getTrainingMode() == 1) {
            //降序
            Collections.sort(list2, new Comparator<ChannelAchievementReturnVo>() {
                @Override
                public int compare(ChannelAchievementReturnVo o1, ChannelAchievementReturnVo o2) {
                    if (StringUtils.isEmpty(o1.getAchieivement())) {
                        return 1;
                    }
                    if (StringUtils.isEmpty(o2.getAchieivement())) {
                        return -1;
                    }
                    try {
                        return Integer.parseInt(o2.getAchieivement()) - Integer.parseInt(o1.getAchieivement());
                    } catch (Exception e) {
                        return -1;
                    }
                }
            });
            return AjaxResult.success(list2);
        } else {
            //升序
            Collections.sort(list2, new Comparator<ChannelAchievementReturnVo>() {
                @Override
                public int compare(ChannelAchievementReturnVo o1, ChannelAchievementReturnVo o2) {
                    if (StringUtils.isEmpty(o1.getAchieivement())) {
                        return 1;
                    }
                    if (StringUtils.isEmpty(o2.getAchieivement())) {
                        return -1;
                    }
                    return o1.getAchieivement().compareTo(o2.getAchieivement());
                }
            });
            return AjaxResult.success(list2);
        }

    }

    @ApiOperation(value = "水上(陆上)训练——查看训练小组航道详情")
    @PostMapping("/trainingGroup/getChannelDetails")
    public AjaxResult getChannelDetails(@RequestBody TrainingGroupChannelVo trainingGroupChannelVo) throws ParseException {

        // 增加相应的航道信息 1个航道1的,2个航道2的,3个航道3的
        List<TrainingGroupChannel> groupChannels = trainingGroupChannelService.getTrainingGroupChannelByGroupId(trainingGroupChannelVo.getGroupId(),false);
        Integer trainingDayId = trainingGroupService.getTrainingDayIdByGroupId(trainingGroupChannelVo.getGroupId());
        Map<String, Object> map2 = new HashMap<>();
        map2.put("weekId", trainingGroupChannelVo.getWeekId());
        map2.put("trainingDayId", trainingDayId);
        map2.put("groupId", trainingGroupChannelVo.getGroupId());
        for (TrainingGroupChannel trainingGroupChannel : groupChannels) {
            map2.put("channelId", trainingGroupChannel.getId());
            List<TrainingAchievement> trainingAchievements = trainingAchievementService.trainingAchievementList(map2);
            trainingGroupChannel.setPerson(trainingAchievements);
        }
        Map<Integer, List<TrainingGroupChannel>> map = groupChannels.stream().collect(Collectors.toMap(TrainingGroupChannel::getBatch,
                p -> {
                    List<TrainingGroupChannel> itemList = new ArrayList<>();
                    itemList.add(p);
                    return itemList;
                },
                (List<TrainingGroupChannel> value1, List<TrainingGroupChannel> value2) -> {
                    value1.addAll(value2);
                    return value1;
                }
        ));
        List<TrainingChannelGroupListVo> list2 = new ArrayList<>();
        for (Map.Entry<Integer, List<TrainingGroupChannel>> entry : map.entrySet()) {
            if (entry.getKey() != null) {
                TrainingChannelGroupListVo trainingChannelGroupListVo = new TrainingChannelGroupListVo();
                trainingChannelGroupListVo.setBatch(entry.getKey());
                trainingChannelGroupListVo.setGroupChannels(entry.getValue());
                for (TrainingGroupChannel trainingGroupChannel : entry.getValue()) {
                    //没有船为路上项目
                    if (trainingGroupChannel.getBoatType() != null) {

                        DevInfo devInfo = redisCache.getCacheObject(RedisConstants.Dev_Info_Prefix + trainingGroupChannel.getShipSpeedId());
                        if(devInfo==null){
                            trainingGroupChannel.setShipSpeedStatus(0);
                        }else{
                            trainingGroupChannel.setShipSpeedStatus(1);
                            trainingGroupChannel.setShipSpeed(devInfo.getSpeed());
//                            trainingGroupChannel.setShipSpeed(shipSpeedRecordLogService.qryCurrentSpeed(trainingGroupChannel.getShipSpeedId()));
                       }


                       /* Integer size = shipSpeedRecordLogService.list(Wrappers.<ShipSpeedRecordLog>lambdaQuery().eq(ShipSpeedRecordLog::getShipSpeedSn , trainingGroupChannel.getShipSpeedId())).size();
                        if(size > 0){
                        //获取船速仪状态
                        Integer status = redisCache.getCacheObject(RedisConstants.ShipSpeed_Status_Prefix + trainingGroupChannel.getShipSpeedId());
                        if (status != null && status == 1) {
                            trainingGroupChannel.setShipSpeedStatus(1);
                            trainingGroupChannel.setShipSpeed(shipSpeedRecordLogService.qryCurrentSpeed(trainingGroupChannel.getShipSpeedId()));
                        } else {
                            trainingGroupChannel.setShipSpeed(0);
                        }
                        }*/
                    }
                    for (TrainingAchievement trainingAchievement : trainingGroupChannel.getPerson()) {
                        if (trainingAchievement.getSportsmanId() != null) {
                            String sportsmanName = trainingAchievementService.selectSportsmanNameById(trainingAchievement.getSportsmanId());
                            trainingAchievement.setSportsmanName(sportsmanName);

                            // 新加字段数据 心率字段
                            trainingAchievement.setHeartRate(String.valueOf(sportsmanService.getHeartCount(trainingAchievement.getSportsmanId())));
                        }
                    }
                }
                list2.add(trainingChannelGroupListVo);
            }
        }
        return AjaxResult.success(list2);
    }


    @ApiOperation(value = "水上(陆上)训练——修改训练小组")
    @PostMapping("/trainingGroup/edit")
    public AjaxResult editTrainingGroup(@RequestBody TrainingGroup trainingGroup) {
        trainingGroupService.updateById(trainingGroup);
        return AjaxResult.success();
    }

    @ApiOperation(value = "水上(陆上)训练——删除训练小组")
    @GetMapping("/trainingGroup/remove")
    public AjaxResult removeTrainingGroup(@RequestParam(value = "id", required = false) Integer id) {
        trainingGroupService.removeById(id);
        return AjaxResult.success();
    }

    @ApiOperation(value = "水上(陆上)训练——查询训练小组航道列表")
    @GetMapping("/trainingGroupChannel/list")
    public TableDataInfo listTrainingGroupChannel(TrainingGroupChannel trainingGroupChannel) {
        startPage();
        List<TrainingGroupChannel> list = trainingGroupChannelService.selectTrainingGroupChannelList(trainingGroupChannel);
        return getDataTable(list);
    }

    @ApiOperation(value = "水上(陆上)训练——查询训练小组航道详细信息")
    @GetMapping("/trainingGroupChannel/getInfo/{id}")
    public AjaxResult getTrainingGroupChannelInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingGroupChannelService.getById(id));
    }

    @ApiOperation(value = "水上(陆上)训练——保存成绩")
    @PostMapping("/trainingGroupChannel/saveTrainingAchievement")
    public AjaxResult saveTrainingAchievement(@RequestBody TrainingGroupChannelVo trainingGroupChannelVo) {
        TrainingGroup trainingGroup = trainingGroupService.getById(trainingGroupChannelVo.getGroupId());

        for (TrainingGroupChannel trainingGroupChannel : trainingGroupChannelVo.getTrainingGroupChannels()) {
            TrainingGroupChannel channelTemp = trainingGroupChannelService.getById(trainingGroupChannel.getId());
            trainingGroupChannel.setBatch(channelTemp.getBatch());
            trainingGroupChannel.setChannelName(channelTemp.getChannelName());
            trainingGroupChannelService.updateTrainingGroupChannelById(trainingGroupChannel);
            Map<String, Object> map3 = new HashMap<>();
            map3.put("groupId", trainingGroupChannelVo.getGroupId());
            map3.put("channelName", trainingGroupChannel.getChannelName());
            map3.put("batch", trainingGroupChannel.getBatch());
            Integer channelId2 = trainingGroupChannelService.getChannelIdByGroupIdAndChannelNameAndBatch(map3);
            TrainingAchievement trainingAchievement2 = new TrainingAchievement();
            trainingAchievement2.setWeekId(trainingGroupChannelVo.getWeekId());
            trainingAchievement2.setChannelId(channelId2);
            trainingAchievement2.setGroupId(trainingGroupChannelVo.getGroupId());
            trainingAchievement2.setTrainingDayId(trainingGroup.getTrainingDayId());
            trainingAchievementService.deleteTrainingAchievement(trainingAchievement2);
            for (TrainingAchievement trainingAchievement : trainingGroupChannel.getPerson()) {
                trainingAchievement.setCtime(new Date());
                trainingAchievement.setWeekId(trainingGroupChannelVo.getWeekId());
                trainingAchievement.setChannelId(channelId2);
                trainingAchievement.setGroupId(trainingGroupChannelVo.getGroupId());
                trainingAchievement.setTrainingDayId(trainingGroup.getTrainingDayId());
                trainingAchievementService.save(trainingAchievement);
            }
            //获取小组实际公里数(第一批次，第一航道的实际划行数) [公里数维度，时间维度暂时忽略]
            if (trainingGroupChannel.getChannelName().equals("0") && trainingGroupChannel.getBatch() == 0) {
                trainingGroup.setKilometer(channelTemp.getRowingKilometers());
                trainingGroupService.updateById(trainingGroup);
                //异步汇总训练项目数据
                trainingDayService.aggregateData(trainingGroup.getTrainingDayId());
            }
        }
        return AjaxResult.success();
    }


    @ApiOperation(value = "水上(陆上)训练——新增训练小组航道")
    @PostMapping("/trainingGroupChannel/add")
    public AjaxResult addTrainingGroupChannel(@RequestBody TrainingGroupChannelVo trainingGroupChannelVo) throws ParseException {
        int trainingDayId = trainingGroupService.getTrainingDayIdByGroupId(trainingGroupChannelVo.getGroupId());
        for (TrainingGroupChannel trainingGroupChannel : trainingGroupChannelVo.getTrainingGroupChannels()) {
            trainingGroupChannel.setCtime(new Date());
            trainingGroupChannel.setGroupId(trainingGroupChannelVo.getGroupId());
            if (trainingGroupChannel.getId() == -1) {
                trainingGroupChannelService.save(trainingGroupChannel);
                for (TrainingAchievement trainingAchievement : trainingGroupChannel.getPerson()) {
                    //根据小组id、小组名、批次查询航道id放到成绩对象中存到成绩表中
                    Map<String, Object> map3 = new HashMap<>();
                    map3.put("groupId", trainingGroupChannel.getGroupId());
                    map3.put("channelName", trainingGroupChannel.getChannelName());
                    map3.put("batch", trainingGroupChannel.getBatch());
                    Integer channelId = trainingGroupChannelService.getChannelIdByGroupIdAndChannelNameAndBatch(map3);

                    trainingAchievement.setWeekId(trainingGroupChannelVo.getWeekId());
                    trainingAchievement.setChannelId(channelId);
                    trainingAchievement.setGroupId(trainingGroupChannelVo.getGroupId());
                    trainingAchievement.setTrainingDayId(trainingDayId);
                    trainingAchievement.setCtime(new Date());
                    trainingAchievementService.save(trainingAchievement);
                }
            } else {
                trainingGroupChannelService.updateTrainingGroupChannelById(trainingGroupChannel);
                Map<String, Object> map3 = new HashMap<>();
                map3.put("groupId", trainingGroupChannel.getGroupId());
                map3.put("channelName", trainingGroupChannel.getChannelName());
                map3.put("batch", trainingGroupChannel.getBatch());
                Integer channelId2 = trainingGroupChannelService.getChannelIdByGroupIdAndChannelNameAndBatch(map3);
                TrainingAchievement trainingAchievement2 = new TrainingAchievement();
                trainingAchievement2.setWeekId(trainingGroupChannelVo.getWeekId());
                trainingAchievement2.setChannelId(channelId2);
                trainingAchievement2.setGroupId(trainingGroupChannelVo.getGroupId());
                trainingAchievement2.setTrainingDayId(trainingDayId);
                trainingAchievementService.deleteTrainingAchievement(trainingAchievement2);
                for (TrainingAchievement trainingAchievement : trainingGroupChannel.getPerson()) {
                    trainingAchievement.setWeekId(trainingGroupChannelVo.getWeekId());
                    trainingAchievement.setChannelId(channelId2);
                    trainingAchievement.setGroupId(trainingGroupChannelVo.getGroupId());
                    trainingAchievement.setTrainingDayId(trainingDayId);
                    trainingAchievement.setCtime(new Date());
                    trainingAchievementService.save(trainingAchievement);
                }
            }
        }
        Map<String, Object> map4 = new HashMap<>();
        map4.put("groupId", trainingGroupChannelVo.getGroupId());
        return AjaxResult.success(trainingGroupChannelService.getTrainingGroupChannelByWeekIdAndTrainingDayId(map4));
    }

    @ApiOperation(value = "水上(陆上)训练——修改训练小组航道")
    @PostMapping("/trainingGroupChannel/edit")
    public AjaxResult editTrainingGroupChannel(@RequestBody TrainingGroupChannel trainingGroupChannel) {
        trainingGroupChannelService.updateById(trainingGroupChannel);
        return AjaxResult.success();
    }


    @ApiOperation(value = "水上(陆上)训练——删除训练小组航道")
    @GetMapping("/trainingGroupChannel/{ids}")
    public AjaxResult removeTrainingGroupChannel(@PathVariable Integer[] ids) {
        trainingGroupChannelService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @ApiOperation(value = "本周计划")
    @GetMapping("/getWeekDetails")
    public AjaxResult getWeekDetails(int weekId) {
        return AjaxResult.success(trainingWeekService.getWeekDetails(weekId));
    }

    @ApiOperation(value = "查看周计划信息")
    @GetMapping("/getWeekInfo/{id}")
    public AjaxResult getWeekInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingWeekService.getTrainingWeekById(id));
    }

    @ApiOperation(value = "修改周计划信息")
    @PostMapping("/editWeekInfo")
    public AjaxResult editWeekInfo(@RequestBody TrainingWeek trainingWeek) {
        trainingWeekService.saveOrUpdate(trainingWeek);
        return AjaxResult.success();
    }

    @ApiOperation(value = "训练数据趋势图-各一级科目训练时间占比")
    @PostMapping("/getTrainingDayWeekOrDayVo")
    public AjaxResult getTrainingDayWeekOrDayVo(@RequestBody Map<String, Object> params) {
        return AjaxResult.success(trainingDayService.getTrainingDayWeekOrDayVo(params));
    }

    @ApiOperation(value = "训练数据趋势图-水陆训练量占比")
    @PostMapping("/getTrainingDayKilometerWeekOrDayVo")
    public AjaxResult getTrainingDayKilometerWeekOrDayVo(@RequestBody Map<String, Object> params) {
        return AjaxResult.success(trainingDayService.getTrainingDayKilometerWeekOrDayVo(params));
    }

    @ApiOperation(value = "训练数据趋势图-水陆一级科目下二级科目训练时间占比")
    @PostMapping("/getTrainingDaySecondLevelHourWeekOrDayVo")
    public AjaxResult getTrainingDaySecondLevelHourWeekOrDayVo(@RequestBody TrainingDaySencondLevelParamsVo trainingDaySencondLevelParamsVo) {
        Map<String, Object> params1 = new HashMap<>();
        if (trainingDaySencondLevelParamsVo.getId() != null) {
            params1.put("id", trainingDaySencondLevelParamsVo.getId());
        }
        if (trainingDaySencondLevelParamsVo.getDay() != null) {
            params1.put("day", trainingDaySencondLevelParamsVo.getDay());
        }
        params1.put("firstLevel", "special_training_water");
        List list1 = trainingDayService.getTrainingDaySecondLevelHourWeekOrDayVo(params1);

        Map<String, Object> params2 = new HashMap<>();
        if (trainingDaySencondLevelParamsVo.getId() != null) {
            params2.put("id", trainingDaySencondLevelParamsVo.getId());
        }
        if (trainingDaySencondLevelParamsVo.getDay() != null) {
            params2.put("day", trainingDaySencondLevelParamsVo.getDay());
        }
        params2.put("firstLevel", "land_specific_training_dynamometer");
        List list2 = trainingDayService.getTrainingDaySecondLevelHourWeekOrDayVo(params2);

        Map<String, Object> params3 = new HashMap<>();
        if (trainingDaySencondLevelParamsVo.getId() != null) {
            params3.put("id", trainingDaySencondLevelParamsVo.getId());
        }
        if (trainingDaySencondLevelParamsVo.getDay() != null) {
            params3.put("day", trainingDaySencondLevelParamsVo.getDay());
        }
        params3.put("firstLevel", "land_strength_training");
        List list3 = trainingDayService.getTrainingDaySecondLevelHourWeekOrDayVo(params3);

        Map<String, Object> params4 = new HashMap<>();
        if (trainingDaySencondLevelParamsVo.getId() != null) {
            params4.put("id", trainingDaySencondLevelParamsVo.getId());
        }
        if (trainingDaySencondLevelParamsVo.getDay() != null) {
            params4.put("day", trainingDaySencondLevelParamsVo.getDay());
        }
        params4.put("firstLevel", "aerobic_training_land");
        List list4 = trainingDayService.getTrainingDaySecondLevelHourWeekOrDayVo(params4);

        Map<String, Object> params5 = new HashMap<>();
        if (trainingDaySencondLevelParamsVo.getId() != null) {
            params5.put("id", trainingDaySencondLevelParamsVo.getId());
        }
        if (trainingDaySencondLevelParamsVo.getDay() != null) {
            params5.put("day", trainingDaySencondLevelParamsVo.getDay());
        }
        params5.put("firstLevel", "regeneration_training");
        List list5 = trainingDayService.getTrainingDaySecondLevelHourWeekOrDayVo(params5);

        Map<String, Object> params6 = new HashMap<>();
        if (trainingDaySencondLevelParamsVo.getId() != null) {
            params6.put("id", trainingDaySencondLevelParamsVo.getId());
        }
        if (trainingDaySencondLevelParamsVo.getDay() != null) {
            params6.put("day", trainingDaySencondLevelParamsVo.getDay());
        }
        params6.put("firstLevel", "injury_treatment");
        List list6 = trainingDayService.getTrainingDaySecondLevelHourWeekOrDayVo(params6);

        Map<String, Object> params7 = new HashMap<>();
        if (trainingDaySencondLevelParamsVo.getId() != null) {
            params7.put("id", trainingDaySencondLevelParamsVo.getId());
        }
        if (trainingDaySencondLevelParamsVo.getDay() != null) {
            params7.put("day", trainingDaySencondLevelParamsVo.getDay());
        }
        params7.put("firstLevel", "physical_preparation_training");
        List list7 = trainingDayService.getTrainingDaySecondLevelHourWeekOrDayVo(params7);

        Map<String, Object> params8 = new HashMap<>();
        if (trainingDaySencondLevelParamsVo.getId() != null) {
            params8.put("id", trainingDaySencondLevelParamsVo.getId());
        }
        if (trainingDaySencondLevelParamsVo.getDay() != null) {
            params8.put("day", trainingDaySencondLevelParamsVo.getDay());
        }
        params8.put("firstLevel", "no_training");
        List list8 = trainingDayService.getTrainingDaySecondLevelHourWeekOrDayVo(params8);

        List listVos = new ArrayList();
        listVos.add(new TrainingDayReturnVo("special_training_water", list1));
        listVos.add(new TrainingDayReturnVo("land_specific_training_dynamometer", list2));
        listVos.add(new TrainingDayReturnVo("land_strength_training", list3));
        listVos.add(new TrainingDayReturnVo("aerobic_training_land", list4));
        listVos.add(new TrainingDayReturnVo("regeneration_training", list5));
        listVos.add(new TrainingDayReturnVo("injury_treatment", list6));
        listVos.add(new TrainingDayReturnVo("physical_preparation_training", list7));
//        Map<String,Object> map = new HashMap<>();
//        map.put("special_training_water",list1);
//        map.put("land_specific_training_dynamometer",list2);
//        map.put("land_strength_training",list3);
//        map.put("aerobic_training_land",list4);
//        map.put("regeneration_training",list5);
//        map.put("injury_treatment",list6);
//        map.put("physical_preparation_training",list7);
//        map.put("no_training",list8);
        return AjaxResult.success(listVos);
    }

    @ApiOperation(value = "训练数据趋势图-水陆一级科目下二级科目训练公里数占比")
    @PostMapping("/getTrainingDayKilometerSecondLevelWeekOrDayVo")
    public AjaxResult getTrainingDayKilometerSecondLevelWeekOrDayVo(@RequestBody Map<String, Object> params) {
        return AjaxResult.success(trainingDayService.getTrainingDayKilometerSecondLevelWeekOrDayVo(params));
    }

    @ApiOperation(value = "训练数据趋势图-今日或本周训练完成度对比")
    @PostMapping("/getWeekClassHourAndKilometersVoList")
    public AjaxResult getWeekClassHourAndKilometersVoList(@RequestBody Map<String, Object> params) {
        List<WeekClassHourAndKilometersVo> listVos = trainingWeekService.getWeekClassHourAndKilometersVoList(params);
        for (WeekClassHourAndKilometersVo weekClassHourAndKilometersVo : listVos) {
            params.put("firstLevel", weekClassHourAndKilometersVo.getFirstLevel());
            AnnualProject annualProject = annualProjectService.selectAnnualProject(params);
            if (annualProject == null) continue;
            weekClassHourAndKilometersVo.setAnnualProjectClassHour((annualProject.getClassHour()));
            weekClassHourAndKilometersVo.setAnnualProjectKilometers(annualProject.getSumKilometer()==null?0:annualProject.getSumKilometer());
        }
        return AjaxResult.success(listVos);
    }

    @ApiOperation(value = "训练数据趋势图-今日考勤")
    @GetMapping("/getLeaveFormByNowDate")
    public AjaxResult getLeaveFormByNowDate(@RequestParam String day) {
        Map<String, Object> params = new HashMap<>();
        params.put("startTime", day);
        params.put("endTime", day);
        return AjaxResult.success(leaveFormService.getLeaveFormByNowDate(params));
    }

    @ApiOperation(value = "训练数据趋势图-本周考勤")
    @GetMapping("/getLeaveFormByWeek")
    public AjaxResult getLeaveFormByWeek(@RequestParam int weekId) {
        TrainingWeek trainingWeek = trainingWeekService.getTrainingWeekById(weekId);
        return AjaxResult.success(leaveFormService.getLeaveFormByWeek(trainingWeek));
    }


    /**
     * =========================  教练员端新接口 ==============================
     */


    /**
     * ApiWeekPlanController 接口
     */
    @Autowired
    private ApiWeekPlanService apiWeekPlanService;


    @ApiOperation("教练员端 -- 水上(陆上)训练——新增训练小组航道")
    @RequestMapping(value = "/addChannel", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addChannel(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.addChannel(jsonObject);
    }


    @ApiOperation("教练员端 -- 保存人艇选择 -- 到航道")
    @RequestMapping(value = "/addHumanBoat", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addHumanBoat(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.addHumanBoat(jsonObject);
    }


    @ApiOperation("教练员端 -- 复制上一组数据")
    @RequestMapping(value = "/copyGoUpGroup", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult copyGoUpGroup(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.copyGoUpGroup(jsonObject);
    }


    @ApiOperation("教练员端 -- 查询小组的成绩")
    @RequestMapping(value = "/getGroupPerformance", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getGroupPerformance(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.getGroupPerformance(jsonObject);
    }


    @ApiOperation("教练员端 -- 根据航道 id 查询航道的人")
    @RequestMapping(value = "/getChannelIdList", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getChannelIdList(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.getChannelIdList(jsonObject);
    }


    @ApiOperation("教练员端 -- 根据组 id 查询所有的航道")
    @RequestMapping(value = "/getGroupChannelList", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getGroupChannelList(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.getGroupChannelList(jsonObject);
    }


    @ApiOperation("教练员端 -- 根据航道id，运动员 id，分组 id，查询 20 条心率")
    @RequestMapping(value = "/getHeartRateList", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getHeartRateList(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.getHeartRateList(jsonObject);
    }


    @ApiOperation("教练员端 -- 根据航道id，运动员 id，分组 id，查询 20 条 船速仪")
    @RequestMapping(value = "/getShipSpeedList", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getShipSpeedList(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.getShipSpeedList(jsonObject);
    }


    //废弃
    @ApiOperation("教练员端 -- 查询水上专项实时数据 1，船速 2，浆频 3，心率")
    @RequestMapping(value = "/getWaterList", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getWaterList(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.getWaterList(jsonObject);
    }

    @ApiOperation("教练员端 -- 查询水上专项实时数据 1，船速 2，浆频 3，心率")
    @RequestMapping(value = "/getWaterDataList", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getWaterDataList(@RequestBody JSONObject jsonObject) {
        String shipSpeedId = null;
        int channelId = 0;
        int sportsmanId = 0;
        if(jsonObject.get("shipSpeedId")!=null){
            shipSpeedId = jsonObject.get("shipSpeedId").toString();
        }
        if(jsonObject.get("channelId")!=null && jsonObject.get("sportsmanId")!=null){
            channelId = Integer.parseInt(jsonObject.get("channelId").toString());
            sportsmanId = Integer.parseInt(jsonObject.get("sportsmanId").toString());
        }else{
            return AjaxResult.success();
        }
        List<GetWaterDataVO> retList = apiWeekPlanService.getWaterDataList(shipSpeedId,channelId,sportsmanId);
        return AjaxResult.success(retList);
    }

    @ApiOperation(value = "获取船速仪的实时数据 船速，距离，桨频")
    @GetMapping("/getCurrentWaterDataList")
    public AjaxResult queryCurrentShipSpeed(HttpServletRequest request) {
        int channelId = 0;
        try {
            channelId = Integer.parseInt(request.getParameter("channelId"));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return AjaxResult.error("参数错误");
        }
        List<CurrentShipSpeedVo> list = apiWeekPlanService.selectRealTimeDataByChannelId(channelId);
        return AjaxResult.success(list);
    }

    @ApiOperation("教练员端 -- 提交成绩")
    @RequestMapping(value = "/submitResults", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult submitResults(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.submitResults(jsonObject);
    }


    @ApiOperation("教练员端 -- 点击计次按钮")
    @RequestMapping(value = "/countTimes", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult countTimes(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.countTimes(jsonObject);
    }


    @ApiOperation("教练员端 -- 查询计次")
    @RequestMapping(value = "/getCountTimes", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getCountTimes(@RequestBody JSONObject jsonObject) {
        List<HeartRateCountVo> list = apiWeekPlanService.getCountTimes(jsonObject);
        return AjaxResult.success(list);
    }


    @ApiOperation("教练员端 -- 测功仪数据 --- 查询分段成绩")
    @RequestMapping(value = "/getSubsection", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getSubsection(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.getSubsection(jsonObject);
    }


    @ApiOperation("教练员端 -- 本周数据查询列表 -- 训练计划总表")
    @RequestMapping(value = "/getWeeklyPlanListVO", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getWeeklyPlanListVO(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.getWeeklyPlanListVO(jsonObject);
    }


    @ApiOperation("教练员端 -- 查询训练课质量")
    @RequestMapping(value = "/getTrainingCourseQuality", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getTrainingCourseQuality() {
        return apiWeekPlanService.getTrainingCourseQuality();
    }


    @ApiOperation("教练员端 -- 根据课程 id 修改课程质量")
    @RequestMapping(value = "/updateQualityById", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateQualityById(@RequestBody JSONObject jsonObject) {
        return apiWeekPlanService.updateQualityById(jsonObject);
    }


}
